-- tests scene
local layer = nil

-- 生成一个对话框(msg:"显示文本", btnTable:{"按钮1","按钮2"}, funcTable{按钮1回调,按钮2回调}, showpot:显示坐标[ccp(x,y)])
function CCDialog(msg, btnTable, funcTable, argsTable, showpot)

    layer = CCLayerColor:create(ccc4(0,0,0,80))
    
    -- 默认参数
    if btnTable == nil then
        btnTable = {"确定"}
    end
    if argsTable == nil then
        argsTable = {}
    end
    if showpot == nil then
        showpot = ccp(DWinSizeWidth/2, DWinSizeHeight*0.7)
    end
    if funcTable == nil then
        funcTable = {}
    end
    
    
    
    local function menuCallback(tag)
        print(tag)
        local menufunc = CloseSceneDialog--默认为关闭此对话框
        if funcTable[tag] ~= nil then
            menufunc = funcTable[tag]
        end
        menufunc(argsTable[tag])
        --print(menufunc, tag, funcTable, argsTable)
        
        Play_Effect(sme_Click)
    end
    
    
    -- 背景//遮挡按钮用
    local _Menubg = CCMenu:create()
    -- 主菜单大小
    _Menubg:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    -- 设置坐标
    _Menubg:setPosition(0, 0)
    layer:addChild(_Menubg,1)

    local btnItem = nil
    btnItem = CCMenuItemLabel:create(CCLabelTTF:create("", "Arial",FontSize))
    btnItem:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    btnItem:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    _Menubg:addChild(btnItem,100)
    
    -- 背景图片
    local _bgBrick = CCSprite:createWithSpriteFrameName(s_Space_sbg)
    _bgBrick:setAnchorPoint(ccp(0.5, 0.5))
    _bgBrick:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2 + 120))
    layer:addChild(_bgBrick)
    
    
    -- 文本
    local _lbMessage = CCLabelTTF:create(msg, "Arial",FontSize)
    _lbMessage:setPosition(gdccp(showpot.x, showpot.y-30))
    layer:addChild(_lbMessage)
    ------ 按钮
    
    local btnwidth = 100
    local btpx = showpot.x - (#btnTable - 1) * btnwidth / 2
    local btpy = showpot.y - 150
    for index, labelName in pairs(btnTable) do
        local btnItem = GFunc_CreateButtonP("rst_enter.png", menuCallback)
        btnItem:setPosition(gdccp(btpx + ((index-1) * btnwidth), btpy))
        _Menubg:addChild(btnItem, index, index)
    end
    
    
    local function onTouchEnded(x, y)
    end

    local function onTouch(eventType, x, y)
    end
    
    layer:setTouchEnabled(true)
    layer:registerScriptTouchHandler(onTouch)
    
    --添加至当前显示的的界面中
    CCDirector:sharedDirector():getRunningScene():addChild(layer,0,MessageUI)
end

function CCDialogClose(msg, btnTable, funcTable, argsTable, showpot)
    CloseSceneDialog()
    CCDialog(msg, btnTable, funcTable, argsTable, showpot)
end


local layerInfo = nil

function HideGameInfos()
    if layerInfo ~= nil then
        CCDirector:sharedDirector():getRunningScene():removeChild(layerInfo, true)
        layerInfo = nil
    end
end

function ShowGameInfos()
    layerInfo = CCLayerColor:create(ccc4(0,0,0,80))
    
    local function menuCallback()
        HideGameInfos()
    end
    
    
    -- 背景//遮挡按钮用
    local _Menubg = CCMenu:create()
    -- 主菜单大小
    _Menubg:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    -- 设置坐标
    _Menubg:setPosition(0, 0)
    layerInfo:addChild(_Menubg,1)

    local btnItem = nil
    btnItem = CCMenuItemLabel:create(CCLabelTTF:create("", "Arial",FontSize))
    btnItem:setPosition(gdccp(0, 0))
    btnItem:setAnchorPoint(ccp(0, 0))
    btnItem:setContentSize(CCSizeMake(DWinSize.width, DWinSize.height))
    btnItem:registerScriptTapHandler(menuCallback)
    _Menubg:addChild(btnItem,100)
    
    -- 背景图片
    local _bgBrick = CCSprite:createWithSpriteFrameName("UIjiemianditu_002.png")
    _bgBrick:setAnchorPoint(ccp(0.5, 0.5))
    _bgBrick:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    _bgBrick:setScaleX(DWinSize.width*0.85/_bgBrick:getContentSize().width)
    _bgBrick:setScaleY(DWinSize.height*0.85/_bgBrick:getContentSize().height)
    layerInfo:addChild(_bgBrick)
    
    -- 文本
    local _lbMessage = CCLabelTTF:create("游戏简介：\
    古老的帝国被阴谋者推翻，年轻的王子远\n走他乡。十年后，王子率领着大军返回自己\n的故乡，然而却被无数奇怪的方块阻拦着他\n回家的道路，为了国家、为了人民，王子毅\n然挥起了手中的长剑……\
\
游戏操作：\
有三种操作方式\
　　魂力无双：点击屏幕砖块手指左右上下\n滑动，组合成三个以上砖块消除\
　　水晶时空：点击屏幕砖块手指移动到相\n应位置，组合成三个以上砖块消除\
　　挑战模式：点击屏幕三个以上相连砖块\n，进行消除即可操纵角色击败敌人\
    \
通过消除砖块积累分数、魂力来兑换水晶币\n换取更好的装备，战斗吧勇士！为帝国而战！\
", "Arial",FontSize, CCSizeMake(gdv(480), gdv(800)), kCCTextAlignmentLeft)
    _lbMessage:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2.2))
    --_lbMessage:setContentSize(CCSizeMake(gdv(460), gdv(760)))
    layerInfo:addChild(_lbMessage)
    
    local function onTouch(eventType, x, y)
        HideGameInfos()
    end
    layerInfo:setTouchEnabled(true)
    layerInfo:registerScriptTouchHandler(onTouch)
    
    --添加至当前显示的的界面中
    CCDirector:sharedDirector():getRunningScene():addChild(layerInfo)
end











function CCDialogNews(msg, func)
    local layerNews = nil
    
    local function touchBegin()
        if func then
            func()
        end
        
        CCDirector:sharedDirector():getRunningScene():removeChild(layerNews, true)
    end
    
    layerNews = GFunc_CreateLayerEnterOrExit(ccc4(0,0,0,80), nil, nil, nil, touchBegin, nil, nil, nil, true)
    
    -- 默认参数
    if btnTable == nil then
        btnTable = {}
        --btnTable = {"确定"}
    end
    if argsTable == nil then
        argsTable = {}
    end
    if showpot == nil then
        showpot = ccp(DWinSizeWidth/2, DWinSizeHeight*0.7)
    end
    if funcTable == nil then
        funcTable = {}
    end
    
    -- 默认参数
    -- 背景图片
    local _bgBrick = CCSprite:createWithSpriteFrameName(s_Space_sbg)
    _bgBrick:setAnchorPoint(ccp(0.5, 0.5))
    _bgBrick:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2 + 120))
    layerNews:addChild(_bgBrick)
    
    
    -- 文本
    local _lbMessage = CCLabelTTF:create(msg, "Arial", FontSize4, CCSizeMake(gdv(300), gdv(150)), kCCTextAlignmentCenter)
    _lbMessage:setPosition(gdccp(showpot.x, showpot.y-80))
    layerNews:addChild(_lbMessage)
    
    
    --添加至当前显示的的界面中
    CCDirector:sharedDirector():getRunningScene():addChild(layerNews)
end


